package com.visikard.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.visikard.ui.R;

public class RedeemScanFragment extends BaseFragment {

	public static final String TAG_SCAN_FRAG = "ScanCodeFragment";

	private LinearLayout lyCodeAbow, lyCodeBelow;

	private Animation swipeAnimationTop = null;
	private Animation swipeAnimationDown = null;
	private ImageView imgBack;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.redeem_scan_fragment, container, false);

		lyCodeAbow = (LinearLayout) view.findViewById(R.id.layout_scancode_abow);
		lyCodeBelow = (LinearLayout) view.findViewById(R.id.layout_scancode_below);

		swipeAnimationTop = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_kard_out_to_top);
		swipeAnimationTop.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				lyCodeAbow.setVisibility(View.GONE);
			}
		});

		swipeAnimationDown = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_kard_out_to_down);
		swipeAnimationDown.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
			}

			@Override
			public void onAnimationEnd(Animation animation) {
				lyCodeBelow.setVisibility(View.GONE);
				getActivity().getSupportFragmentManager().beginTransaction()
						.replace(R.id.scan_container, new ScanCodeFragment(), TAG_SCAN_FRAG).commit();
			}
		});

		imgBack = (ImageView) view.findViewById(R.id.img_back);
		imgBack.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				doBackPressed();
			}
		});

		view.postDelayed(new Runnable() {

			@Override
			public void run() {
				lyCodeAbow.startAnimation(swipeAnimationTop);
				lyCodeBelow.startAnimation(swipeAnimationDown);
			}
		}, 1000);

		return view;
	}

	protected void doBackPressed() {
		FragmentManager fMan = getActivity().getSupportFragmentManager();
		if (fMan.getBackStackEntryCount() > 0) {
			fMan.popBackStackImmediate();
		}
	}

}
